<template>
    <div class="merchant-list">
        <el-card class="mb-4">
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="区域名称">
                    <el-input v-model="searchForm.areaName" placeholder="请输入区域名称" clearable class="w-60" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSearch">查询</el-button>
                    <el-button @click="resetSearch">重置</el-button>
                    <el-button type="primary" @click="handleAdd" style="margin-left: 30px"
                        v-has="'shop:floor:mallArea:save'">新增区域</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    
        <!-- 表格 -->
        <el-table v-loading="loading" :data="tableData" border style="width: 100%;margin-top:15px">
            <el-table-column prop="areaId" label="序号" width="80" />
            <el-table-column prop="areaName" label="区域名称" />
            <el-table-column prop="description" label="区域描述" />
            <el-table-column prop="sortOrder" label="排序" width="100" />
            <!-- <el-table-column prop="floors" label="楼层列表" width="170" /> -->
            <el-table-column label="操作" width="150" fixed="right">
                <template #default="{ row }">
                    <el-button type="text" v-has="'shop:floor:mallArea:update'" @click="handleEdit(row)">编辑</el-button>
                    <el-button type="text" style="color: #f56c6c" v-has="'shop:floor:mallArea:delete'"
                        @click="handleDelete(row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page.sync="currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"
                layout="total, sizes, prev, pager, next" :total="total">
            </el-pagination>
        </div>
    
        <!-- 新增/编辑弹窗 -->
        <el-dialog :close-on-click-modal="false"  :title="dialogType === 'add' ? '新增' : '编辑'" :visible.sync="dialogVisible" width="80%"
            @close="resetForm">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="120px" class="dialog_height">
                <el-form-item label="区域名称" prop="areaName">
                    <el-input v-model="form.areaName" placeholder="请输入区域名称" />
                </el-form-item>
                <el-form-item label="区域描述" prop="description">
                    <el-input v-model="form.description" placeholder="请输入区域描述" />
                </el-form-item>
                <el-form-item label="排序" prop="sortOrder">
                    <el-input v-model="form.sortOrder" placeholder="请输入排序" />
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleSubmit">确定</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'MerchantList',
  data() {
    return {
      // 搜索表单
      searchForm: {
        areaName: '',
      },
      // 分页
      currentPage: 1,
      pageSize: 10,
      total: 0,
      // 表格数据
      tableData: [],
      loading: false,
      // 弹窗相关
      dialogVisible: false,
      dialogType: 'add', // add 或 edit
      uploadUrl: '', // 上传地址
      form: {
        areaName: '',
        description: '',
        sortOrder: '',
      },
      // 表单校验规则
      rules: {
        areaName: [{ required: true, message: '请输入区域名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入区域描述', trigger: 'blur' }],
        sortOrder: [{ required: true, message: '请输入排序', trigger: 'blur' }],
      },
      merchantList: []
    }
  },
  created() {
    this.getList();
    this.queryUserList()
  },
  methods: {
    async queryUserList() {
      const { data } = await this.$api.getUserLists({
        page: 1,
        size: 1000,
        condition: {
          nickname: '',
          phone: '',
          status: '',
          type: 3
        }
      })
      this.merchantList = data.result.records
      console.log(data, '商户列表');
    },
    // 获取列表数据
    async getList() {
      this.loading = true
      try {
        const { data } = await this.$api.getMallAreaList({
          page: this.currentPage,
          size: this.pageSize,
          condition: {
            ...this.searchForm
          }
        })
        this.tableData = data.result.records
        this.total = data.result.total
      } catch (error) {
        console.error('获取商家列表失败：', error)
      } finally {
        this.loading = false
      }
    },

    // 搜索
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },

    // 重置搜索
    resetSearch() {
      this.searchForm = {
        areaName: '',
      }
      this.handleSearch()
    },

    // 分页相关
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 新增
    handleAdd() {
      this.dialogType = 'add'
      this.dialogVisible = true
    },

    // 编辑
    handleEdit(row) {
      this.dialogType = 'edit'
      this.form = { ...row }
      this.dialogVisible = true
    },

    // 删除
    handleDelete(row) {
      this.$confirm('确认删除吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          await this.$api.deleteMallArea(row.areaId)
          this.$message.success('删除成功')
          this.getList()
        } catch (error) {
          console.error('删除失败：', error)
        }
      }).catch(() => { })
    },


    // 提交表单
    handleSubmit() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          try {
            if (this.dialogType === 'add') {
              await this.$api.addMallArea(this.form)
              this.$message.success('新增成功')
            } else {
              const params = {
                ...this.form,
                id: this.form.areaId
              }
              delete params.areaId
              await this.$api.editMallArea(params)
              this.$message.success('编辑成功')
            }
            this.dialogVisible = false
            this.getList()
          } catch (error) {
            console.error(this.dialogType === 'add' ? '新增失败：' : '编辑失败：', error)
          }
        }
      })
    },

    // 重置表单
    resetForm() {
      if (this.$refs.formRef) {
        this.$refs.formRef.resetFields()
      }
      this.form = {
        name: '',
        title: '',
        imageUrl: '',
        address: '',
        phone: '',
        description: ''
      }
    }
  }
}
</script>

<style scoped lang="scss">
.dialog_height{
    height: 50vh;
    overflow: hidden;
    overflow-y: auto;
}
.image-uploader {
    .uploaded-image {
        width: 140px;
        height: 140px;
        object-fit: cover;
        border-radius: 4px;
    }

    .upload-placeholder {
        width: 140px;
        height: 140px;
        border: 1px dashed #d9d9d9;
        border-radius: 4px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        transition: border-color 0.3s;

        &:hover {
            border-color: #409EFF;
        }

        .el-icon-plus {
            font-size: 28px;
            color: #8c939d;
        }

        .upload-text {
            font-size: 12px;
            color: #8c939d;
            margin-top: 8px;
        }
    }
}

.merchant-list {
    padding: 20px;
}

.pagination {
    margin-top: 20px;
    text-align: right;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
}

.avatar {
    width: 120px;
    height: 120px;
    display: block;
}
</style>